/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ModelExpressionGroundTruthEditor.java
 *
 * Created on Sep 10, 2012, 2:25:39 PM
 */

package br.usp.ime.faguilar.guis;

import DatabaseMathExpressions.ExpressionLevelGroundTruth;
import DatabaseMathExpressions.ModelExpressionGroundTruth;

/**
 *
 * @author frank
 */
public class ModelExpressionGroundTruthEditor extends javax.swing.JPanel {
    private ModelExpressionGroundTruth groundTruth;
    private int selectedIndex;
    private static final int DEFAUL_INDEX = 0;

    /** Creates new form ModelExpressionGroundTruthEditor */
    public ModelExpressionGroundTruthEditor() {
        initComponents();
        initializeCustomFields();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        textArea = new javax.swing.JTextArea();
        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        isContentMathMLCheckBox = new javax.swing.JCheckBox();
        jPanel3 = new javax.swing.JPanel();
        deleteElement = new javax.swing.JButton();
        previousElement = new javax.swing.JButton();
        nextelement = new javax.swing.JButton();
        newElement = new javax.swing.JButton();

        setLayout(new java.awt.BorderLayout());

        textArea.setColumns(20);
        textArea.setRows(5);
        textArea.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                textAreaKeyReleased(evt);
            }
        });
        jScrollPane1.setViewportView(textArea);

        add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jPanel1.setLayout(new java.awt.BorderLayout());

        isContentMathMLCheckBox.setText("Is Content MathML ");
        isContentMathMLCheckBox.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                isContentMathMLCheckBoxStateChanged(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .add(isContentMathMLCheckBox)
                .addContainerGap(174, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(isContentMathMLCheckBox)
        );

        jPanel1.add(jPanel2, java.awt.BorderLayout.NORTH);

        deleteElement.setText("-");
        deleteElement.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                deleteElementMouseClicked(evt);
            }
        });
        jPanel3.add(deleteElement);

        previousElement.setText("<");
        previousElement.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                previousElementMouseClicked(evt);
            }
        });
        jPanel3.add(previousElement);

        nextelement.setText(">");
        nextelement.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                nextelementMouseClicked(evt);
            }
        });
        jPanel3.add(nextelement);

        newElement.setText("+");
        newElement.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                newElementMouseClicked(evt);
            }
        });
        jPanel3.add(newElement);

        jPanel1.add(jPanel3, java.awt.BorderLayout.CENTER);

        add(jPanel1, java.awt.BorderLayout.PAGE_END);
    }// </editor-fold>//GEN-END:initComponents

    private void previousElementMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_previousElementMouseClicked
        // TODO add your handling code here:
        if(isValidIndex(getSelectedIndex() - 1)){
            updateSelectedIndex(getSelectedIndex() - 1);
        }
    }//GEN-LAST:event_previousElementMouseClicked

    private void nextelementMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_nextelementMouseClicked
        // TODO add your handling code here:
        if(isValidIndex(getSelectedIndex() + 1)){
            updateSelectedIndex(getSelectedIndex() + 1);
        }
    }//GEN-LAST:event_nextelementMouseClicked

    private void updateSelectedIndex(int index){
        setSelectedIndex(index);
        textArea.setText(getGroundTruth().get(getSelectedIndex()).getGroundTruth());
        isContentMathMLCheckBox.setSelected(getGroundTruth().get(getSelectedIndex()).isContentMathML());
    }


    private void newElementMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_newElementMouseClicked
        // TODO add your handling code here:
        addNewGroundTruth();
    }//GEN-LAST:event_newElementMouseClicked

    private void deleteElementMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_deleteElementMouseClicked
        // TODO add your handling code here:
        deleteCurrentGroundTruthElement();
    }//GEN-LAST:event_deleteElementMouseClicked

    private void textAreaKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_textAreaKeyReleased
        // TODO add your handling code here:

//        groundTruth.set(getSelectedIndex(), getExpressionLevelGrounTruth());
        groundTruth.get(getSelectedIndex()).setGroundTruth(textArea.getText());
    }//GEN-LAST:event_textAreaKeyReleased

    private void isContentMathMLCheckBoxStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_isContentMathMLCheckBoxStateChanged
        // TODO add your handling code here:
        boolean isContentMathML = isContentMathMLCheckBox.isSelected();
        groundTruth.get(getSelectedIndex()).setContentMathML(isContentMathML);
    }//GEN-LAST:event_isContentMathMLCheckBoxStateChanged

//    private ExpressionLevelGroundTruth getExpressionLevelGrounTruth(){
//        boolean isContentMathML = jCheckBox1.isSelected();
//        String groundTruth = textArea.getText();
//        ExpressionLevelGroundTruth expressionLevelGroundTruth =  new ExpressionLevelGroundTruth();
//        expressionLevelGroundTruth.setContentMathML(isContentMathML);
//        expressionLevelGroundTruth.setGroundTruth(groundTruth);
//        return expressionLevelGroundTruth;
//    }

    private void deleteCurrentGroundTruthElement(){
        if(!groundTruth.isEmpty()){
            groundTruth.remove(getSelectedIndex());
            if(groundTruth.isEmpty()){
                addNewGroundTruth();
            }else{
                if(getSelectedIndex() < getGroundTruth().size())
                    updateSelectedIndex(getSelectedIndex());
                else
                    updateSelectedIndex(getSelectedIndex()-1);
            }
        }
    }

    private boolean isValidIndex(int index){
        if(index >= 0 && index < groundTruth.size())
            return true;
        return false;
    }

    private void addNewGroundTruth(){
        groundTruth.add(new ExpressionLevelGroundTruth());
        selectedIndex = groundTruth.size()-1;
        textArea.setText("");
        isContentMathMLCheckBox.setSelected(groundTruth.get(selectedIndex).isContentMathML());
    }

    public int getSelectedIndex() {
        return selectedIndex;
    }

    public void setSelectedIndex(int selectedIndex) {
        this.selectedIndex = selectedIndex;
    }



    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton deleteElement;
    private javax.swing.JCheckBox isContentMathMLCheckBox;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton newElement;
    private javax.swing.JButton nextelement;
    private javax.swing.JButton previousElement;
    private javax.swing.JTextArea textArea;
    // End of variables declaration//GEN-END:variables


    public void initializeCustomFields(){
        setGroundTruth(new ModelExpressionGroundTruth());
        addNewGroundTruth();
//        setSelectedIndex(INDEX_FOR_EMPTY_GROUND_TRUTH);
    }

    public ModelExpressionGroundTruth getGroundTruth() {
        return groundTruth;
    }

    public void setGroundTruth(ModelExpressionGroundTruth goundTruth) {
        this.groundTruth = goundTruth;
        if(!goundTruth.isEmpty()){
            textArea.setText(goundTruth.get(DEFAUL_INDEX).getGroundTruth());
            selectedIndex = DEFAUL_INDEX;
        }
    }

}
